package com.toymate.modules.oss.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * OssController
 *
 * @author weirdor (179946422@qq.com)
 * @version v1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/oss")
public class OssController {

    @PostMapping("/upload")
    public String handleUpload(@RequestBody byte[] data, HttpServletRequest request) throws IOException {
        String dateStr = DateUtil.today();
        String path = dateStr + "/";
        String fileName = IdUtil.simpleUUID() + StrUtil.DOT + "wav";
        // 构造文件保存路径
        String filePath = path + fileName;
        //创建文件
        File file = FileUtil.touch(filePath);
        FileUtil.writeBytes(data,file);

        return file.getAbsolutePath();
    }

}
